
/// <reference path="./../Define.ts" />
/// <reference path="./../Main.ts" />
/// <reference path="./../module/ModuleMgr.ts" />
/// <reference path="./../module/BaseModule.ts" />


namespace apeng {

    @moduleMgr.add(EModuleType.language)
    export class LanguageModule extends BaseModuleEvent {

        public readonly EventType = {
            CHANGE: "CHANGE",
        }

        public cur: ELanguageType = null!
        public curStr: string = null!

        private _datas: { [id: number]: IConfigLanguage } = {}

        onCreate() {
            this.updateType()

            // 集合语言表数据
            for (let configName in ConfigFileName)
                if (configName.indexOf("language") != -1) {
                    let configs = _config_.obj[configName as "language"]
                    for (let id in configs)
                        this._datas[id] = configs[id]
                }

            let languageDefine = initData.languageDefine as any
            if (languageDefine && languageDefine.chinese) {
                for (let id in languageDefine.chinese) {
                    //@ts-ignore
                    this._datas[id] = [LanguageDefineConfig.chinese[id]]
                }
                for (let key in languageDefine) {
                    let index = ELanguageType[key as any]

                    if (index) {
                        let datas = languageDefine[key as "chinese"]
                        for (let id in datas) {
                            //@ts-ignore
                            this._datas[id][index] = datas[id]
                        }
                    }
                }

                this.updateType()

            }
        }

        private updateType() {
            this.curStr = initData.getLanguageType()
            this.cur = ELanguageType[this.curStr]
        }

        public change(type: ELanguageType) {
            if (initData.getLanguageType() == ELanguageType[type]) {
                // UIMgr.tip("是当前语言无需切换")
                return
            }
            // UIMgr.Loading.wait(true)
            this.setLanguageType(type)
            this.updateType()


            console.log("切换语言" + this.curStr)

            // UIMgr
            //     .closeAll(EUILayer.Panel)
            //     .closeAll(EUILayer.Notice)

            this.emitQueue(this.EventType.CHANGE, -1, () => {
                console.log("切换成功")

                // TimerMgr.once(this, () => {
                //     UIMgr.Loading.wait(false)
                // }, .3)

            })
            // PlatformMgr.reStart()
        }


        /**设置当前的语言id */
        public setLanguageType(type: ELanguageType) {
            localStorage.setItem("language", type + "")
        }

        /**通过lang配置表获取对应的文本 */
        public get<T extends string>(id: number): T {
            let obj = this._datas[id] as any
            if (!obj)
                return "" as any
            return obj[this.cur]
        }

        public replace(id: number, str: string): string {
            return this.get(id).replace("{0}", str)
        }

        /**获取语言对应图片的路径 */
        public getSpriteFrame(url: string): string {
            if (this.cur == ELanguageType.chinese)
                return url
            return "languageTexture/" + this.curStr + "/" + url
        }

        /**格式化图片路径 */
        public converSpriteFrameUrl(url: string) {
            let index = url.indexOf("languageTexture/")
            if (index == -1)
                return url

            // "languageTexture/".length=16 +1 =17
            index = url.indexOf("/", index + 17)
            return url.substring(index + 1)
        }


    }
}